<template>
  <a-tree-select size="large"
                 :value="value"
                 :multiple="multiple"
                 :max-tag-count="maxTagCount"
                 :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                 :tree-data="treeData"
                 :disabled="disabled"
                 allowClear
                 placeholder="请选择行政区"
                 show-search
                 :filterTreeNode="filterMethod"
                 @change="handleChange">
  </a-tree-select>
</template>

<script>
export default {
  name: "SAdcdTree",
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: [String, Array],
      default: function () {
        return getTemporaryInfo("USER_DETAIL").adcds
          ? getTemporaryInfo("USER_DETAIL").adcds[0]
          : "";
      }
    },
    pid: {
      type: String,
      // default: "652800000000000",
      default: function () {
        return getTemporaryInfo("USER_DETAIL").adcds
          ? getTemporaryInfo("USER_DETAIL").adcds[0]
          : "652800000000000";
      }
    },
    multiple: {
      type: Boolean,
      default: false
    },
    maxTagCount: {
      type: Number
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      treeData: []
      // adcd: this.value || null
    };
  },
  created () {
    this.getData();
  },
  methods: {
    filterMethod (str, node) {
      return ~(node.data.props.title || "").indexOf(str);
    },
    getData () {
      const params = {
        isQueryAll: true,
        pid: this.pid
      }
      this.$http.userauth.then(res => {
          const parseNode = item => {
            return {
              title: item.title,
              value: item.value,
              key: item.value,
              children: item.children && item.children.map(parseNode),
              isLeaf: item.children && !item.children.length
            };
          };

          if (!res) {
            return;
          }
          this.treeData = [parseNode(res[0])];
        });
    },
    handleChange (val) {
      // this.$emit("input", val);
      this.$emit("change", val);
      this.$emit("change-done", val);
    }
  }
};
</script>
